package zd.cornermemory.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.lang.reflect.Method;

import zd.cornermemory.R;
import zd.cornermemory.utils.SPUtils;
import zd.cornermemory.utils.SpKey;

/**
 * webview
 */
public class HTMLActivity extends BaseActivity {
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html);
        setBackImage();

        webView = (WebView) findViewById(R.id.web);
        //获取浏览器设置
        WebSettings webSettings = webView.getSettings();
        //允许javascript
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("utf-8");//文本编码
        String url = "file:///android_asset/index.html";
//        webView.loadUrl(url);
        //本地HTML里面有跨域的请求 原生webview需要设置之后才能实现跨域请求
        if (Build.VERSION.SDK_INT >= 16) {
            webSettings.setAllowUniversalAccessFromFileURLs(true);
        }else{
            try {
                Class<?> clazz = webSettings.getClass();
                Method method = clazz.getMethod(
                        "setAllowUniversalAccessFromFileURLs", boolean.class);
                if (method != null) {
                    method.invoke(webSettings, true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        webView.addJavascriptInterface(this, "androidFuction");

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public static final String HTML_KEY = "html_key";
    @JavascriptInterface
    public void setLocalValue(String key,String value) {
        SPUtils.getInstance().put(HTML_KEY + key, value);
        if (TextUtils.equals(key, "code")) {//编码
            changeCodeToJava(value);
        }
        if (TextUtils.equals(key, "codeinput")) {//坐标
            changeInputToJava(value);
        }
    }

    @JavascriptInterface
    public void removeLocalValue(String key) {
        SPUtils.getInstance().remove(HTML_KEY + key);
    }


    @JavascriptInterface
    public String getLocalValue(String key){
        String value = SPUtils.getInstance().getString(HTML_KEY + key, "");
        return value;
    }

    //DEGCGAAJWIXKOOMREDCXTQLMKHIRZZPSBBLSQNJYHFFYWTNP
    private void changeCodeToJava(String value){
        char[] c = value.toCharArray();

        String UP_CORNER = "" + c[2] + c[7] + c[5] + c[0];
        String UP_EDGE = "" + c[1] + c[4] + c[6] + c[3];
        String DOWN_CORNER = "" + c[10] + c[15] + c[13] + c[8];
        String DOWN_EDGE = "" + c[9] + c[12] + c[14] + c[11];

        String LEFT_CORNER = "" + c[18] + c[23] + c[21] + c[16];
        String LEFT_EDGE = "" + c[17] + c[20] + c[22] + c[19];
        String RIGHT_CORNER = "" + c[26] + c[31] + c[29] + c[24];
        String RIGHT_EDGE = "" + c[25] + c[28] + c[30] + c[27];

        String FRONT_CORNER = "" + c[34] + c[39] + c[37] + c[32];
        String FRONT_EDGE = "" + c[33] + c[36] + c[38] + c[35];
        String BACK_CORNER = "" + c[42] + c[47] + c[45] + c[40];
        String BACK_EDGE = "" + c[41] + c[44] + c[46] + c[43];


        SPUtils.getInstance().put(SpKey.UP_CORNER,UP_CORNER);
        SPUtils.getInstance().put(SpKey.UP_EDGE,UP_EDGE);
        SPUtils.getInstance().put(SpKey.DOWN_CORNER,DOWN_CORNER);
        SPUtils.getInstance().put(SpKey.DOWN_EDGE,DOWN_EDGE);

        SPUtils.getInstance().put(SpKey.LEFT_CORNER,LEFT_CORNER);
        SPUtils.getInstance().put(SpKey.LEFT_EDGE,LEFT_EDGE);
        SPUtils.getInstance().put(SpKey.RIGHT_CORNER,RIGHT_CORNER);
        SPUtils.getInstance().put(SpKey.RIGHT_EDGE,RIGHT_EDGE);

        SPUtils.getInstance().put(SpKey.FRONT_CORNER,FRONT_CORNER);
        SPUtils.getInstance().put(SpKey.FRONT_EDGE,FRONT_EDGE);
        SPUtils.getInstance().put(SpKey.BACK_CORNER,BACK_CORNER);
        SPUtils.getInstance().put(SpKey.BACK_EDGE,BACK_EDGE);


        Intent broadcast = new Intent(SpKey.MODIFY_CODE_SUCCESS);
        sendBroadcast(broadcast);

    }

    private void changeCodeToJs(){



    }

    /**
     * 黄1  白2  蓝3  绿4  红5  橙6
     *
     * 13 代表黄顶蓝前        41 代表绿顶黄前
     * 14 代表黄顶绿前        42 代表绿顶白前
     * 15 代表黄顶红前        45 代表绿顶红前
     * 16 代表黄顶橙前        46 代表绿顶橙前
     *
     * 23 代表白顶蓝前        51 代表红顶黄前
     * 24 代表白顶绿前        52 代表红顶白前
     * 25 代表白顶红前        53 代表红顶蓝前
     * 26 代表白顶橙前        54 代表红顶绿前
     *
     * 35 代表蓝顶红前        61 代表橙顶黄前
     * 36 代表蓝顶橙前        62 代表橙顶白前
     * 31 代表蓝顶黄前        63 代表橙顶蓝前
     * 32 代表蓝顶白前        64 代表橙顶绿前
     *
     */
    private void changeInputToJava(String value){
        String str = "15";
        switch (value) {
            case "1":
                str = "15";break;
            case "2":
                str = "16";break;
            case "3":
                str = "13";break;
            case "4":
                str = "14";break;
            case "5":
                str = "25";break;
            case "6":
                str = "26";break;
            case "7":
                str = "23";break;
            case "8":
                str = "24";break;
            case "9":
                str = "35";break;
            case "10":
                str = "36";break;
            case "11":
                str = "31";break;
            case "12":
                str = "32";break;
            case "13":
                str = "45";break;
            case "14":
                str = "46";break;
            case "15":
                str = "41";break;
            case "16":
                str = "42";break;
            case "17":
                str = "53";break;
            case "18":
                str = "54";break;
            case "19":
                str = "51";break;
            case "20":
                str = "52";break;
            case "21":
                str = "63";break;
            case "22":
                str = "64";break;
            case "23":
                str = "61";break;
            case "24":
                str = "62";break;

        }
        SPUtils.getInstance().put(SpKey.COORDINATE,str);
        Intent broadcast = new Intent(SpKey.MODIFY_COORNIATE_SUCCESS);
        sendBroadcast(broadcast);

    }

    private void changeInputToJs(){



    }

}
